package com.fdb.efp.nls.service.impl.hjwp;

import com.fdb.basic.framework.core.util.DateUtility;
import org.apache.commons.lang3.StringUtils;

import java.io.*;

/**
 * 同步dat,ok文件: <br>
 * 0.1:caoxin:2019/7/3 11:29: TODO: <br>
 *
 * @author caoxin
 * @version 0.1
 * @date 2019/7/3 11:29
 * @since 2019/7/3 11:29
 */
public interface SynFileDataInterface {
    String PLACEHOLDER = "$date$";

    default String getCurrentBatchFileName(String fileName, String date10) {
        String date8 = DateUtility.format10To8(date10);
        return StringUtils.replace(fileName, PLACEHOLDER, date8);
    }

    default int getOkFileDataNum(String prefix, String fileName, String date10){
        String okFile = prefix + getCurrentBatchFileName(fileName, date10);
        try (BufferedReader fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(okFile)))) {
            String line = null;
            while ((line = fileReader.readLine()) != null) {
                return Integer.valueOf(StringUtils.substring(line, line.lastIndexOf("=") + 1));
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException("【找不到对应的文件】" + okFile);
        } catch (IOException e) {
            throw new RuntimeException("【读取文件异常】" + okFile);
        } catch (Exception e) {
            throw new RuntimeException("【文件格式不正确】" + okFile);
        }
        return -1;
    }

    default boolean validateOkFile(int size, String prefix, String fileName, String date10) {
        String okFile = prefix + getCurrentBatchFileName(fileName, date10);
        try (BufferedReader fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(okFile)))) {
            String line = null;
            while ((line = fileReader.readLine()) != null) {
                return Integer.valueOf(StringUtils.substring(line, line.lastIndexOf("=") + 1)).equals(size);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException("【找不到对应的文件】" + okFile);
        } catch (IOException e) {
            throw new RuntimeException("【读取文件异常】" + okFile);
        } catch (Exception e) {
            throw new RuntimeException("【文件格式不正确】" + okFile);
        }
        return false;
    }

    /**
     * 根据文件路径获取文件内容的行数
     *
     * @param fileUrl
     * @return
     */
    default int getFileNum(String fileUrl) {

        FileReader fileReader = null;
        LineNumberReader lineNumberReader = null;
        try {
            final File file = new File(fileUrl);
            if (file.exists()) {
                fileReader = new FileReader(file);
                lineNumberReader = new LineNumberReader(fileReader);
                lineNumberReader.skip(Long.MAX_VALUE);
                return lineNumberReader.getLineNumber();
            }
            return -1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        } finally {
            if (lineNumberReader != null) {
                try {
                    lineNumberReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
